package org.example.demo1.servlets.ManagePage;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.demo1.Entity.User;
import org.example.demo1.utils.MybatisUtils;
import org.example.demo1.DAO.UserDao;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/API/user/list")
public class UserListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置响应内容类型
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        
        // 获取分页参数
        int pageNum = 1;
        int pageSize = 10; // 每页10条数据
        
        try {
            // 获取请求中的分页参数
            String pageNumStr = req.getParameter("pageNum");
            if (pageNumStr != null && !pageNumStr.isEmpty()) {
                pageNum = Integer.parseInt(pageNumStr);
            }
            
            // 获取查询条件
            String username = req.getParameter("username");
            String phone = req.getParameter("phone");
            String status = req.getParameter("status");
            // 移除角色相关代码
            // String role_id = req.getParameter("role_id");
            
            // 计算偏移量
            int offset = (pageNum - 1) * pageSize;
            
            // 查询数据库
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            
            // 构建查询条件
            Map<String, Object> params = new HashMap<>();
            params.put("offset", offset);
            params.put("pageSize", pageSize);
            
            if (username != null && !username.isEmpty()) {
                params.put("username", "%" + username + "%");
            }
            if (phone != null && !phone.isEmpty()) {
                params.put("phone", "%" + phone + "%");
            }
            if (status != null && !status.isEmpty()) {
                params.put("status", Integer.parseInt(status));
            }
            // 移除角色相关代码
            // if (role_id != null && !role_id.isEmpty()) {
            //     params.put("role_id", Integer.parseInt(role_id));
            // }
            
            // 查询用户列表
            List<User> users = userDao.getUserListPagination(params);
            
            // 查询总记录数
            int total = userDao.getUserCount(params);
            
            // 构建响应数据
            Map<String, Object> result = new HashMap<>();
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("total", total);
            result.put("rows", users);
            
            // 创建配置了序列化null值的Gson实例
            Gson gson = new GsonBuilder()
                    .serializeNulls()  // 这行配置会让Gson序列化null值
                    .create();
            
            // 返回JSON数据
            out.write(gson.toJson(result));
            
            // 关闭SqlSession
            sqlSession.close();
        } catch (Exception e) {
            // 处理异常
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("msg", "查询失败：" + e.getMessage());
            
            Gson gson = new Gson();
            out.write(gson.toJson(result));
            
            e.printStackTrace();
        } finally {
            out.close();
        }
    }
}
